/**
 * RExt.grid.RecordTools
 * A plugin that creates an extra column on grid with [edit][delete] link-buttons for each row.
 * @author Chris Scott
 */
RExt.grid.RecordTools = Ext.extend(Ext.Component, {

    /**
     * @cfg {Number} width [100]
     * The width of the column
     */
    width: 100,

    /**
     * @cfg {String} header [Tools]
     * The column header-text
     */
    header : 'Tools',

    initComponent : function() {
        // create events edit & delete
        this.addEvents({
            /**
             * @event edit
             * fired when row [edit] button is clicked
             * return false to cancel the edit.
             * @param {Grid} the grid
             * @param {Record} the record
             */
            'edit' : true,

            /**
             * @event delete
             * fired when row [delete] button is clicked
             * return false to cancel the delete
             * @param {Grid} the grid
             * @param {Record} the record
             */
            'delete' : true
        });

        // super
        RExt.grid.RecordTools.superclass.initComponent.call(this);
    },

    /**
     * init
     * Standard plugin init function, called automatically by Ext framework.  gets grid's ColumnModel and pushes a new column
     * containing [edit][delete] links
     * @param {Object} grid
     */
    init : function(grid) {

        var cm = grid.getColumnModel();
        var config = cm.config;
        config.push({
            id: 'tools',
            header: this.header,
            width: this.width,
            renderer: this.renderTools,
            menuDisabled: true,
            resizable: false,
            sortable: false
        });
        cm.setConfig(config);

        // add cellclick listener.  determine if clicked-cell was a row-tool ([edit][delete]).
        // if so, which tool?  react accordingly.
        grid.on('cellclick', function(g, rIndex, cIndex, ev) {
            var tool = ev.getTarget('a.row-tool', 1, true);
            if (tool) {
                var rec = g.store.getAt(rIndex);
                if (tool.hasClass('btn-edit')) {
                    this.onEdit(g, rec);
                }
                else if (tool.hasClass('btn-delete')) {
                    this.onDelete(g, rec);
                }
            }
        },this);
    },

    /**
     * renderTools
     * Column-renderer implementation
     * @see Ext.grid.ColumnModel::setRenderer
     * value : Object
     * metadata : Object
          o css : String
          o attr : String
     *
     * record : Ext.data.record
     * rowIndex : Number
     * colIndex : Number
     * store : Ext.data.Store

     */
    renderTools : function(value, metadata, record, rIndex, cIndex, store) {
        return '<a class="btn-edit row-tool" href="#">'+App.t('edit') + '</a>&nbsp;<a class="btn-delete row-tool" href="#">' + App.t('delete') + '</a>';
    },

    /**
     * onEdit
     * @param {Grid} grid
     * @param {Ext.data.Record} record
     */
    onEdit : function(grid, record) {
        this.fireEvent('edit', grid, record);
    },

    /**
     * onDelete
     * @param {Grid} grid
     * @param {Ext.data.Record} record
     */
    onDelete : function(grid, record) {
        this.fireEvent('delete', grid, record);
    }
});